package domain.entities;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * Ongoing entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 *
 * @see Ongoing
 * @author MyEclipse Persistence Tools
 */

public class OngoingDAO extends HibernateDaoSupport {
    private static final Logger log = LoggerFactory.getLogger(OngoingDAO.class);
    // property constants
    public static final String ACTUAL_PTS_ACTIVE = "actualPtsActive";
    public static final String ACTUAL_PTS_ENROLLED = "actualPtsEnrolled";
    public static final String ACTUAL_PTS_RANDOMIZED = "actualPtsRandomized";
    public static final String BMS_AUDIT = "bmsAudit";
    public static final String EC_AUDIT = "ecAudit";
    public static final String INSPECTION = "inspection";
    public static final String INSPECTION_ACTION = "inspectionAction";
    public static final String INSPECTION_MOH = "inspectionMoh";
    public static final String LOCAL_APPROVAL_FILE = "localApprovalFile";
    public static final String LOCAL_APPROVED_BY = "localApprovedBy";
    public static final String LOCAL_APPROVED_BY_OTHER = "localApprovedByOther";
    public static final String OUTCOME_BMS = "outcomeBms";
    public static final String OUTCOME_EC = "outcomeEc";
    public static final String SITE_STATUS = "siteStatus";
    public static final String SPECIAL_RECRUITMENT_ACTIONS = "specialRecruitmentActions";
    public static final String SPECIAL_RECRUITMENT_ACTIONS_DESCRIPTION = "specialRecruitmentActionsDescription";

    protected void initDao() {
        // do nothing
    }

    public void save(Ongoing transientInstance) {
        log.debug("saving Ongoing instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(Ongoing persistentInstance) {
        log.debug("deleting Ongoing instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public Ongoing findById(java.lang.Integer id) {
        log.debug("getting Ongoing instance with id: " + id);
        try {
            Query query = getHibernateTemplate().getSessionFactory().openSession().createQuery("from Ongoing where id = " + id);
            Ongoing instance = (Ongoing) query.uniqueResult();
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<Ongoing> findByExample(Ongoing instance) {
        log.debug("finding Ongoing instance by example");
        try {
            List<Ongoing> results = (List<Ongoing>) getHibernateTemplate()
                    .findByExample(instance);
            log.debug("find by example successful, result size: "
                    + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding Ongoing instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from Ongoing as model where model."
                    + propertyName + "= ?";
            return getHibernateTemplate().find(queryString, value);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Ongoing> findByActualPtsActive(Object actualPtsActive) {
        return findByProperty(ACTUAL_PTS_ACTIVE, actualPtsActive);
    }

    public List<Ongoing> findByActualPtsEnrolled(Object actualPtsEnrolled) {
        return findByProperty(ACTUAL_PTS_ENROLLED, actualPtsEnrolled);
    }

    public List<Ongoing> findByActualPtsRandomized(Object actualPtsRandomized) {
        return findByProperty(ACTUAL_PTS_RANDOMIZED, actualPtsRandomized);
    }

    public List<Ongoing> findByBmsAudit(Object bmsAudit) {
        return findByProperty(BMS_AUDIT, bmsAudit);
    }

    public List<Ongoing> findByEcAudit(Object ecAudit) {
        return findByProperty(EC_AUDIT, ecAudit);
    }

    public List<Ongoing> findByInspection(Object inspection) {
        return findByProperty(INSPECTION, inspection);
    }

    public List<Ongoing> findByInspectionAction(Object inspectionAction) {
        return findByProperty(INSPECTION_ACTION, inspectionAction);
    }

    public List<Ongoing> findByInspectionMoh(Object inspectionMoh) {
        return findByProperty(INSPECTION_MOH, inspectionMoh);
    }

    public List<Ongoing> findByLocalApprovalFile(Object localApprovalFile) {
        return findByProperty(LOCAL_APPROVAL_FILE, localApprovalFile);
    }

    public List<Ongoing> findByLocalApprovedBy(Object localApprovedBy) {
        return findByProperty(LOCAL_APPROVED_BY, localApprovedBy);
    }

    public List<Ongoing> findByLocalApprovedByOther(Object localApprovedByOther) {
        return findByProperty(LOCAL_APPROVED_BY_OTHER, localApprovedByOther);
    }

    public List<Ongoing> findByOutcomeBms(Object outcomeBms) {
        return findByProperty(OUTCOME_BMS, outcomeBms);
    }

    public List<Ongoing> findByOutcomeEc(Object outcomeEc) {
        return findByProperty(OUTCOME_EC, outcomeEc);
    }

    public List<Ongoing> findBySiteStatus(Object siteStatus) {
        return findByProperty(SITE_STATUS, siteStatus);
    }

    public List<Ongoing> findBySpecialRecruitmentActions(
            Object specialRecruitmentActions) {
        return findByProperty(SPECIAL_RECRUITMENT_ACTIONS,
                specialRecruitmentActions);
    }

    public List<Ongoing> findBySpecialRecruitmentActionsDescription(
            Object specialRecruitmentActionsDescription) {
        return findByProperty(SPECIAL_RECRUITMENT_ACTIONS_DESCRIPTION,
                specialRecruitmentActionsDescription);
    }

    public List findAll() {
        log.debug("finding all Ongoing instances");
        try {
            String queryString = "from Ongoing";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public Ongoing merge(Ongoing detachedInstance) {
        log.debug("merging Ongoing instance");
        try {
            Ongoing result = (Ongoing) getHibernateTemplate().merge(
                    detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Ongoing instance) {
        log.debug("attaching dirty Ongoing instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(Ongoing instance) {
        log.debug("attaching clean Ongoing instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static OngoingDAO getFromApplicationContext(ApplicationContext ctx) {
        return (OngoingDAO) ctx.getBean("OngoingDAO");
    }

    public ArrayList<Ongoing> findByProtocoloCompromisoPais(ProtocoloCompromisopais pcp) {
        log.debug("finding Ongoing instance with protocolo compromiso pais");
        try {
            String queryString = "from Ongoing where investigadorProtocoloCentro.id.protocoloCentroprotocoloCompromisopaisprotocoloid = '"+pcp.getId().getProtocoloid()+"' AND investigadorProtocoloCentro.id.protocoloCentroprotocoloCompromisopaiscompromisopaisid = "+pcp.getId().getCompromisopaisid();
            return new ArrayList<Ongoing>(getHibernateTemplate().find(queryString));
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }    }
}